#!/usr/bin/python
# -*- coding: utf-8 -*-
#创建SocketServerTCP服务器：

import socket
import time

host = '192.168.123.3' #板子连上路由器之后分配的IP地址
port = 23                #板子的端口号,这个是固定烧写在板子里的,不用动

#设置数据为鼠标,返回鼠标头部标志数据
def getMouseHand():
    tmp = b'\x57\xab\x02'
    return tmp

#设置移动坐标字节部分
def conventPoint(x = 0,y = 0,m = 0):
    bx = b'\x00'
    by = b'\x00'
    bm = b'\x00'
    if (x >0 and x < 127) or (x <0 and x > -127):
        bx = int(x).to_bytes(length=1, byteorder='big', signed=True)
    if (y >0 and y < 127) or (y <0 and y > -127):
        by = int(y).to_bytes(length=1, byteorder='big', signed=True)
    if (m >0 and m < 127) or (m <0 and m > -127):
        bm = int(m).to_bytes(length=1, byteorder='big', signed=True)
    out = bx+by+bm
    return out

#设置鼠标按键是否按下字节部分
def getTouch(l = False,r = False,mk = False):
    tmp = b'\x00'
    if l:
        ltmp = b'\x01'
        tmp = bytes([tmp[0] | ltmp[0]])
    if r:
        rtmp = b'\x02'
        tmp = bytes([tmp[0] | rtmp[0]])
    if mk:
        mtmp = b'\x04'
        tmp = bytes([tmp[0] | mtmp[0]])
    return tmp

def createMouseData(l = False,r = False,mk = False,x = 0,y = 0,m = 0):
    tmp = b'\x5b'
    tmp += getMouseHand()
    tmp += getTouch(l,r,mk)
    tmp += conventPoint(x,y,m)
    tmp += b'\x5d'
    return tmp

def touchLeft():
    tmp = createMouseData(l = True)
    return tmp

def touchRight():
    tmp = createMouseData(r = True)
    return tmp

def moveX(x):
    tmp = createMouseData(x = x)
    return tmp

def moveY(y):
    tmp = createMouseData(y = y)
    return tmp

def moveM(m):
    tmp = createMouseData(m = m)
    return tmp

def realesAll():
    tmp = createMouseData()
    return tmp

def printWithHEX(s):
    outs = ''
    for c in s:
        outs += '%#x '%(c)
    print(outs)

def main():
    client_socket = socket.socket()      # 创建连接板子的客户端socket对象
    client_socket.connect((host, port))  # 连接到板子上

    #释放所有按键
    cmd = realesAll()
    client_socket.send(cmd)

    time.sleep(0.5) #延时500ms

    for i in range(300):
        #鼠标向右移动100像素,
        cmd = createMouseData(x=-10,y=-19)
        client_socket.send(cmd)
        time.sleep(0.008) #延时500ms

    for i in range(30):
        cmd = createMouseData(x=30,y=30)
        client_socket.send(cmd)
        time.sleep(0.008) #延时500ms
    for i in range(20):
        cmd = createMouseData(x=-20,y=-20)
        client_socket.send(cmd)
        time.sleep(0.008) #延时500ms
    time.sleep(1) #延时500ms
    for i in range(10):
        # #按下鼠标左键
        cmd = touchLeft()
        printWithHEX(cmd)
        client_socket.send(cmd)
        time.sleep(0.08) #延时100ms
        #释放所有按键
        cmd = realesAll()
        printWithHEX(cmd)
        client_socket.send(cmd)
        time.sleep(0.08) #延时500ms
    time.sleep(1) #延时500ms
    # #鼠标向左移动100像素,因为x轴正方向是向右,所以为负值
    # cmd = moveX(-100) 
    # client_socket.send(cmd)

    # time.sleep(0.5) #延时500ms

    

    # #鼠标向上移动100像素,y轴正方向是向下的,所以向下是负值
    # cmd = moveY(-100) 
    # client_socket.send(cmd)

    # time.sleep(0.5) #延时500ms

    # #鼠标向下移动100像素,y轴正方向是向下的,所以向上是正值
    # cmd = moveY(100) 
    # client_socket.send(cmd)

    # time.sleep(0.5) #延时500ms

    # #鼠标中轮向上滚动10下
    # cmd = moveM(10) 
    # client_socket.send(cmd)

    # time.sleep(0.5) #延时500ms

    # #鼠标中轮向下滚动10下
    # cmd = moveM(-10) 
    # client_socket.send(cmd)

    # time.sleep(0.5) #延时500ms

def test():
    tmp = createMouseData(l=True)
    print(str(tmp))

if __name__ == '__main__':
    main()
    # test()
